package com.sservice.web.controller.wx;

import com.sservice.banfu.e.SalePerson;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.common.Constants;
import com.sservice.model.dao.DriverMapper;
import com.sservice.model.entity.Driver;
import com.sservice.model.vo.DriverVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by suweiming on 2017/12/27.
 */

@Controller
@RequestMapping("/wx/driver")
public class DriverWxController {

    @Autowired
    private DriverMapper driverMapper;

    @ResponseBody
    @RequestMapping(value = "/upsert", method = RequestMethod.POST)
    public BaseResult get(Driver arg, HttpSession session) {
        SalePerson salePerson = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        int service_res = 0;
        arg.setUserId(salePerson.getId().toString());
        if (arg.getId() != null) { // 修改
            service_res = driverMapper.updateByPrimaryKeySelective(arg);
        } else {
            service_res = driverMapper.insertSelective(arg);
        }
        BaseResult res = new BaseResult(true, "" + service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public BaseResult del(Driver arg) {
        int service_res = driverMapper.deleteByPrimaryKey(arg.getId());
        BaseResult res = new BaseResult(true, "" + service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public BaseResult detail(Driver arg) {
        Driver d = driverMapper.selectByPrimaryKey(arg.getId());
        return new BaseResult(true, d);
    }

    @ResponseBody
    @RequestMapping(value = "/stat/{id}", method = RequestMethod.GET)
    public BaseResult stat(DriverVo arg) {
        Map map = new HashMap();
        int t = driverMapper.queryCount(arg);
        map.put("total", t);
        // 最近3个月到期数据
        arg.setStartDate("");
        arg.setEndDate("");
        int t1 = driverMapper.queryCount(arg);

        map.put("total", t1);
        return new BaseResult(true, map);
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(DriverVo arg) {
        List<DriverVo> list = driverMapper.queryByPage(arg);
        int total = driverMapper.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
